package com.wintor.client;

import java.util.Date;
import com.wintor.shared.FieldVerifier;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class WintorApp implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final WintorEvolutionServiceAsync wintorEvolutionService = GWT
			.create(WintorEvolutionService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		final TextBox freqTextBox = new TextBox();
		final TextBox durTextBox = new TextBox();
		final TextBox condTextBox = new TextBox();
		final Label msgLabel = new Label();
		freqTextBox.setText("0");
		freqTextBox.setEnabled(false);
		durTextBox.setText("0");
		durTextBox.setEnabled(false);
		condTextBox.setText("0");
		condTextBox.setEnabled(false);
		final Button startTrackButton = new Button("Start Track");
		startTrackButton.addStyleName("sendButton");
		RootPanel.get("freq").add(freqTextBox);
		RootPanel.get("dur").add(durTextBox);
		RootPanel.get("cond").add(condTextBox);
		RootPanel.get("msg").add(msgLabel);
		RootPanel.get("track").add(startTrackButton);

		// Create a handler for the startTrackButton
		class WintorEvolutionHandler implements ClickHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				int freq = new Integer(freqTextBox.getText()).intValue();
				int dur = new Integer(durTextBox.getText()).intValue();
				int cond = new Integer(condTextBox.getText()).intValue();
				// restrict dur within one month for now
				if (dur > 30) {
					dur = 30;
				}

				wintorEvolutionService.startTrack(freq, 
						                          dur,
						                          cond,
						new AsyncCallback<String>() {
							public void onFailure(Throwable caught) {
								Window.alert(SERVER_ERROR);							
							}

							public void onSuccess(String result) {
								msgLabel.setText("Recorded Successfully: ("+ result +")");						
							}
						});
			}

		}

		// Add a handler to send the name to the server
		WintorEvolutionHandler handler = new WintorEvolutionHandler();
		startTrackButton.addClickHandler(handler);
	}
}
